﻿#region zh-CHS 2006 - 2007 DemoSoft 团队 | en 2006 - 2007 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the G.O.S.E(Game Online Server Engine) for .NET.
//
//                 2006-2007 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Collections.Generic;
using Demo_G.O.S.E.ServerEngine.Util;
using Demo_G.O.S.E.ServerEngine.Common;
#endregion

namespace Demo_G.O.S.E.ServerEngine.Ability.Aura
{
    /// <summary>
    /// 
    /// </summary>
    public class BaseAuraManager
    {
        #region zh-CHS 共有属性 | en Public Properties
        /// <summary>
        /// 给出脚本里面的可运动物体的数量(NPCs、Monster)
        /// </summary>
        public int Count
        {
            get { return m_Auras.Count; }
        }
        #endregion

        #region zh-CHS AuraManager方法 | en Public Methods

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private SafeDictionary<long, BaseAura> m_Auras = new SafeDictionary<long, BaseAura>();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="baseTaxiNode"></param>
        public bool AddAura( long iAuraSerial, BaseAura baseAura )
        {
            if ( baseAura == null )
            {
                Debug.WriteLine( "BaseAuraManager.AddAura(...) - baseAura == null error!" );
                return false;
            }

            EventHandler<BeforeAddAuraCallEventArgs> tempBeforeEventArgs = m_ThreadEventBeforeAddAuraCall;
            if ( tempBeforeEventArgs != null )
            {
                BeforeAddAuraCallEventArgs eventArgs = new BeforeAddAuraCallEventArgs( baseAura, this );
                tempBeforeEventArgs( this, eventArgs );

                if ( eventArgs.IsCancel == true )
                    return false;
            }

            m_Auras.Add( iAuraSerial, baseAura );

            EventHandler<AfterAddAuraCallEventArgs> tempAfterEventArgs = m_ThreadEventAfterAddAuraCall;
            if ( tempAfterEventArgs != null )
            {
                AfterAddAuraCallEventArgs eventArgs = new AfterAddAuraCallEventArgs( baseAura, this );
                tempAfterEventArgs( this, eventArgs );
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool RemoveAura( long iAuraSerial )
        {
            BaseAura baseAura = FindAuraOnSerial( iAuraSerial );
            if ( baseAura == null )
                return false;

            EventHandler<BeforeRemoveAuraCallEventArgs> tempBeforeEventArgs = m_ThreadEventBeforeRemoveAuraCall;
            if ( tempBeforeEventArgs != null )
            {
                BeforeRemoveAuraCallEventArgs eventArgs = new BeforeRemoveAuraCallEventArgs( baseAura, this );
                tempBeforeEventArgs( this, eventArgs );

                if ( eventArgs.IsCancel == true )
                    return false;
            }

            m_Auras.Remove( iAuraSerial );

            EventHandler<AfterRemoveAuraCallEventArgs> tempAfterEventArgs = m_ThreadEventAfterRemoveAuraCall;
            if ( tempAfterEventArgs != null )
            {
                AfterRemoveAuraCallEventArgs eventArgs = new AfterRemoveAuraCallEventArgs( baseAura, this );
                tempAfterEventArgs( this, eventArgs );
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public BaseAura FindAuraOnSerial( long iAuraSerial )
        {
            return m_Auras.GetValue( iAuraSerial );
        }

        /// <summary>
        /// 
        /// </summary>
        [MultiThreadedWarningAttribute( "(Aura)当前的数组是列表临时产生的,不能保存数组用于以后操作:警告!" )]
        public BaseAura[] ToArray()
        {
            return m_Auras.ToArray();
        }

        #endregion

        #region zh-CHS 共有事件 | en Public Event

        #region zh-CHS AddAuraCall事件 | en Public Event
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<BeforeAddAuraCallEventArgs> m_ThreadEventBeforeAddAuraCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventBeforeAddAuraCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<BeforeAddAuraCallEventArgs> ThreadBeforeAddAuraCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeAddAuraCall );
                {
                    m_ThreadEventBeforeAddAuraCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeAddAuraCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeAddAuraCall );
                {
                    m_ThreadEventBeforeAddAuraCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeAddAuraCall );
            }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<AfterAddAuraCallEventArgs> m_ThreadEventAfterAddAuraCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventAfterAddAuraCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<AfterAddAuraCallEventArgs> ThreadAfterAddAuraCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterAddAuraCall );
                {
                    m_ThreadEventAfterAddAuraCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterAddAuraCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterAddAuraCall );
                {
                    m_ThreadEventAfterAddAuraCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterAddAuraCall );
            }
        }
        #endregion

        #region zh-CHS RemoveAuraCall事件 | en Public Event
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<BeforeRemoveAuraCallEventArgs> m_ThreadEventBeforeRemoveAuraCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventBeforeRemoveAuraCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<BeforeRemoveAuraCallEventArgs> ThreadBeforeRemoveAuraCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeRemoveAuraCall );
                {
                    m_ThreadEventBeforeRemoveAuraCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeRemoveAuraCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeRemoveAuraCall );
                {
                    m_ThreadEventBeforeRemoveAuraCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeRemoveAuraCall );
            }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<AfterRemoveAuraCallEventArgs> m_ThreadEventAfterRemoveAuraCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventAfterRemoveAuraCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<AfterRemoveAuraCallEventArgs> ThreadAfterRemoveAuraCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterRemoveAuraCall );
                {
                    m_ThreadEventAfterRemoveAuraCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterRemoveAuraCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterRemoveAuraCall );
                {
                    m_ThreadEventAfterRemoveAuraCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterRemoveAuraCall );
            }
        }
        #endregion

        #endregion
    }
}
#endregion